# https://jzsc.mohurd.gov.cn/data/company
import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding="utf-8")

import requests
import execjs
import json
import urllib3

# 禁用 SSL 警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Connection": "keep-alive",
    "Referer": "https://jzsc.mohurd.gov.cn/data/company",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0",
    "accessToken": "",
    "sec-ch-ua": "\"Chromium\";v=\"142\", \"Microsoft Edge\";v=\"142\", \"Not_A Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "timeout": "30000",
    "v": "231012"
}

cookies = {
    "Hm_lvt_b1b4b9ea61b6f1627192160766a9c55c": "1762587371",
    "Hm_lpvt_b1b4b9ea61b6f1627192160766a9c55c": "1762587371",
    "HMACCOUNT": "9F888295D476CF07"
}

url = "https://jzsc.mohurd.gov.cn/APi/webApi/dataservice/query/comp/list"
params = {
    "pg": "1",
    "pgsz": "15",
    "total": "450"
}

# 禁用代理，避免代理连接错误
proxies = {
    "http": None,
    "https": None,
}

try:
    # 发送请求，禁用 SSL 验证和代理
    response = requests.get(
        url, 
        headers=headers, 
        cookies=cookies, 
        params=params,
        proxies=proxies,
        verify=False,  # 禁用 SSL 证书验证
        timeout=30
    )
    
    print("=" * 50)
    print("加密的响应数据:")
    print(response.text)
    print("=" * 50)
    
    # 读取 JS 文件并执行解密
    with open("15.js", "r", encoding="utf-8") as f:
        js_code = f.read()
    
    # 编译 JS 代码
    ctx = execjs.compile(js_code)
    
    # 调用解密函数
    decrypted_data = ctx.call("decrypter", response.text)
    
    print("\n解密后的数据:")
    print(decrypted_data)
    print("=" * 50)
    
    # 尝试格式化 JSON 输出
    if decrypted_data:
        try:
            json_data = json.loads(decrypted_data)
            print("\n格式化的 JSON 数据:")
            print(json.dumps(json_data, ensure_ascii=False, indent=2))
        except json.JSONDecodeError as e:
            print(f"JSON 解析错误: {e}")
    else:
        print("解密结果为空，请检查密钥和 IV 是否正确")
        
except requests.exceptions.RequestException as e:
    print(f"请求错误: {e}")
except Exception as e:
    print(f"发生错误: {e}")
